package com.yunsc.admin.controller.product;

import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.product.dto.GoodsParamsDTO;
import com.yunsc.product.dto.QueryGoodsDTO;
import com.yunsc.product.service.YunscGoodsService;
import com.yunsc.product.vo.GoodsDetailVO;
import com.yunsc.product.vo.GoodsInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 商品基本信息接口
 * @date: 2024-04-14 20:53
 */
@Api(tags = "商品信息管理", value = "商品基本信息接口")
@RestController
@RequestMapping("goods")
public class GoodsController {

    @Resource
    private YunscGoodsService goodsService;


    /**
     * 查询商品列表
     */
    @ApiOperation("查询商品列表接口")
    @GetMapping("/list")
    public Result<Map<String, Object>> list(QueryGoodsDTO params) {
        if (params == null) {
            params = new QueryGoodsDTO();
        }
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        PageResult<GoodsInfoVO> page = goodsService.queryGoodsList(params, currentStoreId);
        return Result.resultPageList(page);
    }

    // goods/listByIds?goodsIds=1,2,3,4,5
    @ApiOperation("根据商品id列表查询商品列表")
    @GetMapping("/listByIds")
    public Result<Map<String, Object>> listByIds(@RequestParam("goodsIds") String goodsIds) {
        if (goodsIds == null) {
            return Result.error("商品id不能为空");
        }
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();

        List<GoodsInfoVO> goodsList = goodsService.queryGoodsListByIds(goodsIds, currentStoreId);

        return Result.resultList(goodsList);
    }


    /**
     * 根据id查询商品信息
     */
    @ApiOperation("根据id查询商品详细信息")
    @GetMapping("/detail")
    public Result queryGoodsDetail(@RequestParam("goodsId") Integer goodsId) {
        GoodsDetailVO yoshopGoods = goodsService.queryGoodsDetailById(goodsId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsInfo", yoshopGoods);
        return Result.ok(map);
    }


    /**
     * 添加商品
     */
    @ApiOperation("添加商品")
    @PostMapping("/add")
    public Result saveGoodsInfo(@RequestBody GoodsParamsDTO goodsParams) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        goodsService.saveGoodsInfo(goodsParams.getForm(), currentStoreId);

        return Result.ok("添加成功");
    }

    /**
     * 修改
     */
    @ApiOperation("修改商品")
    @PostMapping("/edit")
    public Result editGoodsInfo(@RequestBody @Validated({ValidationGroups.Update.class}) GoodsParamsDTO goodsParams) {
        if (goodsParams.getGoodsId() == null) {
            return Result.error("商品id不能为空");
        }
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        goodsService.editGoodsInfo(goodsParams, currentStoreId);
        return Result.ok("更新成功");
    }

    /**
     * 删除
     */
    @ApiOperation("删除商品")
    @PostMapping("/delete")
    public Result deleteGoodsByIds(@RequestBody @Validated({ValidationGroups.Delete.class}) GoodsParamsDTO goodsParams) {
        goodsService.deleteGoodsByIds(goodsParams.getGoodsIds());
        return Result.ok();
    }

    @ApiOperation("商品上架/下架")
    @PostMapping("/state")
    public Result editGoodsState(@RequestBody @Validated({ValidationGroups.Sate.class}) GoodsParamsDTO goodsParams) {
        goodsService.editGoodsState(goodsParams.getGoodsIds(), goodsParams.isState());
        return Result.ok();
    }


}
